﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Model;
using Ecreate.Business;

namespace Ecreate.Web.usercontrol
{
    public partial class ucLogin : System.Web.UI.UserControl
    {
        protected t_info info = new t_info();
        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {
                InfoService service = new InfoService();
                t_info value = service.getInfo();
                if (value != null)
                {
                    this.info = value;
                }
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string str_username = this.userName.Text;
            string str_password = this.password.Text;
            if (str_username == "")
            {
                return;
            }
            if (str_password == "")
            {
                return;
            }

            if (!utility.Strings.checkString(str_username))
            {
                this.lbl_message.Text = "输入中含有非法字符";
                this.lbl_message.Visible = true;
                return;
            }
            if (!utility.Strings.checkString(str_password))
            {
                this.lbl_message.Text = "输入中含有非法字符";
                this.lbl_message.Visible = true;
                return;
            }
            CustomerService service = new CustomerService();
            WebResult<t_customer> result = service.checkPassword(str_username, str_password);
            if (result.Success == false)
            {
                this.lbl_message.Text = result.Message;
                this.lbl_message.Visible = true;
            }
            else
            {
                utility.SessionManage.AddSessionObject(this.Session, result.ResultObject);
                Response.Redirect("/Default.aspx");
            }

        }
        public bool checkSession()
        {
            bool ret = utility.SessionManage.checkSessoin(this.Session);
            return ret;
        }
        public string getUserName()
        {
            utility.SessionObject value = (utility.SessionObject)utility.SessionManage.getSessionObject(this.Session);

            if (value.Usertype == Ecreate.Web.utility.LoginUserType.customer)
            {
                return value.Customer.Cus_name;
            }
            else
            {
                return "";
            }
        }


        public string getUserID()
        {
            utility.SessionObject value = (utility.SessionObject)utility.SessionManage.getSessionObject(this.Session);

            if (value.Usertype == Ecreate.Web.utility.LoginUserType.customer)
            {
                return value.Customer.Cus_id;
            }
            else
            {
                return "";
            }
        }

        public string getName()
        {
            return this.ClientID;
        }

        protected void btn_reg_Click(object sender, EventArgs e)
        {
            Response.Redirect("/pages/customer_insert.aspx");
        }

        protected void btn_edit_Click(object sender, EventArgs e)
        {
            string url = "/pages/updpassword.aspx?id={0}";
            url = string.Format(url, this.getUserID());
            Response.Redirect(url);
        }

        protected void btn_exit_Click(object sender, EventArgs e)
        {
            utility.SessionManage.ClearSessionObject(this.Session);
            Response.Redirect("/Default.aspx");
        }
    }
}